package com.dingwei.lesyndrome.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.NewFragmentAdapter;
import com.dingwei.lesyndrome.app.MyApplication;
import com.dingwei.lesyndrome.bean.NewsListBean;
import com.dingwei.lesyndrome.mvp.presenter.NewsFragmentPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.dingwei.lesyndrome.ui.activity.NewDetailsActivity;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.utils.ScreenUtils;
import com.lihao.baselibrary.weight.MyRadioGroup;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/26.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 最新动态 - fragment
 */

public class NewsFragment extends LazyLoadFragment implements Contract.NewsFragmentView {

    NewsFragmentPresenter newsFragmentPresenter = new NewsFragmentPresenter(this);
    @Bind(R.id.banner)
    MZBannerView banner;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    NewFragmentAdapter adapter;
    NewsListBean bean;
    private String id;

    //*****************主要代码********************
    //接收数据
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        id = getArguments().getString("id");
        L.e("id = " + getArguments().getString("id"));
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_news;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();

    }

    private void initView() {
        banner.setIndicatorVisible(false);

        LinearLayout.LayoutParams lp = new MyRadioGroup.LayoutParams(ScreenUtils.getScreenWidth(BaseActivity.context), ScreenUtils.getScreenWidth(BaseActivity.context) / 16 * 9);
        banner.setLayoutParams(lp);

        adapter = new NewFragmentAdapter(BaseActivity.context);

        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.bg1), DisplayUtil.dip2px(BaseActivity.context, 8)));

        mRecyclerView.setAdapter(adapter);


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                newsFragmentPresenter.initData(page, id, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                newsFragmentPresenter.initData(page, id, true, false);
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                NewsListBean.DataBean.ListBean bean = adapter.getItem(position);
                intent  = new Intent(BaseActivity.context, NewDetailsActivity.class);
                intent.putExtra("id",bean.id);
                startActivity(intent);
            }
        });
        banner.setBannerPageClickListener(new MZBannerView.BannerPageClickListener() {
            @Override
            public void onPageClick(View view, int i) {
                NewsListBean.DataBean.StickListBean bean1 = bean.data.stickList.get(i);
                intent  = new Intent(BaseActivity.context, NewDetailsActivity.class);
                intent.putExtra("id",bean1.id);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void lazyLoad() {
        page = 1;
        newsFragmentPresenter.initData(page, id, true, true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        banner.pause();//暂停轮播
    }

    @Override
    public void onResume() {
        super.onResume();
        banner.start();//开始轮播
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void onLoadMore(NewsListBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void onRefresh(NewsListBean bean) {
        this.bean = bean;
        banner.setIndicatorVisible(false);
        // 设置数据
        banner.setPages(bean.data.stickList, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        banner.start();

        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData("success");

    }

    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.no_net_btn)
    public void onViewClicked() {
        page = 1;
        newsFragmentPresenter.initData(page, id, true, true);
    }

    public static class BannerViewHolder implements MZViewHolder<NewsListBean.DataBean.StickListBean> {
        private ImageView mImageView;
        private TextView tvTitle;

        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.item_news_banner, null);
            mImageView = (ImageView) view.findViewById(R.id.img);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            return view;
        }

        @Override
        public void onBind(Context context, int position, NewsListBean.DataBean.StickListBean data) {
            // 数据绑定
            Glide.with(BaseActivity.context)
                    .load(data.img)
                    .apply(MyApplication.options_no_transform)
                    .into(mImageView);
            tvTitle.setText(data.title);
        }
    }


}
